package com.pactera.madp.cp.controller.oms.sensitive;

import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.entity.sensitive.SensitiveWordGroupEntity;
import com.pactera.madp.cp.api.model.sensitiveword.RespSensitiveWordGroupIndex;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.ISensitiveWordGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:敏感词分组管理
 * @author: Huayu
 * @time: 2021/1/28 14:00
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sensitivewordgroup")
@Api(value = "sensitivewordgroup", tags = "敏感词分组管理")
public class SensitiveWordGroupController {

    private ISensitiveWordGroupService sensitiveWordGroupServiceImpl;

    /**
     *
     * @description:敏感词分组下拉列表
     * @author: Huayu
     * @time: 2021/1/28 14:06
     */
    @GetMapping("/select")
    @ApiOperation(value = "敏感词分组下拉列表")
    public R sensitiveWordGroupIndex(){
        //判断用户绑定企业信息
        if(CorpContextHolder.getCorpId() == null){
            throw new CheckedException("未选择登录企业，不可操作");
        }
        List<SensitiveWordGroupEntity> sensitiveWordGroupEntityList = sensitiveWordGroupServiceImpl.getSensitiveWordGroupsByCorpId(CorpContextHolder.getCorpId());
        if(sensitiveWordGroupEntityList.isEmpty()){
            return R.ok();
        }
        List<RespSensitiveWordGroupIndex> respSensitiveWordGroupIndexList = new ArrayList<RespSensitiveWordGroupIndex>();
        for (SensitiveWordGroupEntity sensitiveWordGroupEntity:
        sensitiveWordGroupEntityList) {
            RespSensitiveWordGroupIndex respSensitiveWordGroupIndex = new RespSensitiveWordGroupIndex();
            respSensitiveWordGroupIndex.setGroupId(sensitiveWordGroupEntity.getId().toString());
            respSensitiveWordGroupIndex.setGroupName(sensitiveWordGroupEntity.getName());
            respSensitiveWordGroupIndexList.add(respSensitiveWordGroupIndex);
        }
        return R.ok(respSensitiveWordGroupIndexList);
    }

    /**
     *
     * @description:修改敏感词分组
     * @author: Huayu
     * @time: 2021/1/28 16:31
     */
    @SysLog("修改敏感词分组")
    @PutMapping
    @ApiOperation(value = "修改敏感词分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="敏感词组id",dataType ="Integer"),
            @ApiImplicitParam(name="name",value="敏感词组名称",dataType ="String")
    })
    public R sensitiveWordGroupUpdate(Integer groupId, String name){
        //判断用户绑定企业信息
        if(CorpContextHolder.getCorpId() == null){
            throw new CheckedException("未选择登录企业，不可操作");
        }
        if(groupId == null){
            throw new CheckedException("分组id不能为空");
        }
        if(name == null || "".equals(name)){
            throw new CheckedException("分组名称不能为空");
        }
        //检测分组是否重复
        nameIsUnique(name,groupId);
        //数据入表
        Integer i = sensitiveWordGroupServiceImpl.updateSensitiveWordGroupById(groupId,name);
        if(i < 1){
            throw new CheckedException("分组更新失败");
        }
        return  R.ok();
    }

    /**
     *
     * @description:添加敏感词分组
     * @author: Huayu
     * @time: 2021/1/28 16:38
     */
    @SysLog("添加敏感词分组")
    @PostMapping
    @ApiOperation(value = "添加敏感词分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="names",value="敏感词组名称",dataType ="String")
    })
    public R sensitiveWordGroupStore(String names){
//        String name = mapData.get("name").toString();
        if(names == null || names.length() == 0){
            throw new CheckedException("敏感词分组名称 必填");
        }
        //判断用户绑定企业信息
        if(CorpContextHolder.getCorpId() == null){
            throw new CheckedException("未选择登录企业，不可操作");
        }
        //创建
        boolean flag = createSensitiveWordGroup(names);
        if(!flag){
            throw new CheckedException("敏感词分组创建失败");
        }
        return R.ok("敏感词分组创建成功");
    }

    private boolean createSensitiveWordGroup(String name) {
        boolean flag = false;
        Map mapData=new HashMap();
        mapData.put("name",name);
        if(name.split(",") != null){
            flag = sensitiveWordGroupServiceImpl.createSensitiveWordGroups(mapData);
        }else{
            flag = sensitiveWordGroupServiceImpl.createSensitiveWordGroup(mapData);
        }
        return flag;

    }

    private boolean nameIsUnique(String name, Integer id) {
        SensitiveWordGroupEntity sensitiveWordGroupEntity = sensitiveWordGroupServiceImpl.getSensitiveWordGroupByNameCorpId(name,id,CorpContextHolder.getCorpId());
        if(sensitiveWordGroupEntity != null){
            throw new CheckedException("该分组名称已存在");
        }
        return true;
    }

}
